package cn.edu.sjtu.software.sjtucontacts.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.sjtu.software.sjtucontacts.R;
import cn.edu.sjtu.software.sjtucontacts.model.Contact;
import cn.edu.sjtu.software.sjtucontacts.model.Group;
import cn.edu.sjtu.software.sjtucontacts.model.Preference;
import cn.edu.sjtu.software.sjtucontacts.util.ListUtil;
import cn.edu.sjtu.software.sjtucontacts.util.StringUtil;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class AddContactInGroupActivity extends Activity {
	Button okButton;
	Button cancelButton;
	private int currentGroupId;
	private List<Integer> contactIds = new ArrayList<Integer>();
	private ListUtil listUtil = ListUtil.getInstance();
	private List<Integer> contactIdList;
	ArrayList<CheckBox> cbs = new ArrayList<CheckBox>();


//	// private List<String> data = new ArrayList<String>();
//	@Override
//	public void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//
//		SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.contactitemingroup,
//				new String[]{"title","info","img"},
//				new int[]{R.id.addContactInGroupTitle,R.id.addContactInGroupInfo,R.id.addContactInGroupImg});
//		setListAdapter(adapter);
//		ListView listView = getListView();
//		listView.addFooterView(findViewById(R.layout.addcontactingroupmenu));
//	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addcontactingroup);
		
		//Get group id from bundle 
        Bundle bundle = this.getIntent().getExtras();
        currentGroupId = bundle.getInt("groupid");
		
		SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.contactitemingroup,
				new String[]{"name","phone","img"},
				new int[]{R.id.addContactInGroupName,R.id.addContactInGroupPhone,R.id.addContactInGroupImg});
		final ListView listView = (ListView)findViewById(R.id.addContactInGroupList);
		switch(Preference.getInstance().getTheme()){
		case 0:
			listView.setBackgroundColor(0xff000000);
			listView.setCacheColorHint(0xff191919);
			
			break;
		case 1:
			listView.setBackgroundResource(R.drawable.summer_green);
			listView.setCacheColorHint(0x00000000);
			break;
		case 2:
			listView.setBackgroundResource(R.drawable.touhou);
			listView.setCacheColorHint(0x00000000);
			break;
		case 3:
			listView.setBackgroundResource(R.drawable.inter);
			listView.setCacheColorHint(0x00000000);
			break;
		};
		listView.setAdapter(adapter);
		
		okButton = (Button)findViewById(R.id.addContactInGroupConfirmButton);
		okButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//CheckBox cb = (CheckBox)listView.findViewById(R.id.addContactInGroupCheckBox);
				int count = listView.getCount();
				for (int i=0;i<count;i++){
					CheckBox cb = (CheckBox)listView.getChildAt(i).findViewById(R.id.addContactInGroupCheckBox);
					if (cb.isChecked() == true){
						int contactId= contactIdList.get(i);
						//listUtil.changeContactToGroup(contactId, StringUtil.GROUP_NOT_IDENTIFIED_ID, currentGroupId);
						listUtil.findContactById(contactId).setGroupId(currentGroupId);
						PullService.writeGroups(listUtil.getGroupList());
						PullService.writeContacts(listUtil.fetchContactList());
						//listUtil.deleteContactFromGroup(contactIdList.get(i), StringUtil.GROUP_NOT_IDENTIFIED_ID);
					}
				}
				finish();
			}
		});
		cancelButton = (Button) findViewById(R.id.addContactInGroupCancelButton);
		cancelButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
	}
	
	private List<Map<String, Object>> getData() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		List<Group> groupList = listUtil.getGroupList();
		for (Group group:groupList){
			if (group.getId() == StringUtil.GROUP_NOT_IDENTIFIED_ID){
				contactIdList = new ArrayList<Integer>();
				for (int i=0;i<group.getContactList().size();i++){
					contactIdList.add(group.getContactList().get(i));
				}
				System.out.println("contactIdList");
				System.out.println(contactIdList);
				for (int i=0;i<contactIdList.size();++i){
					Contact contact = listUtil.findContactById(contactIdList.get(i));
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("name", contact.getFirstName()+contact.getLastName());
					map.put("phone", contact.getDefaultPhoneNumber());
					map.put("img", R.drawable.default_avatar);
					list.add(map);
					System.out.println("list");
					System.out.println(list);
				}
			}
		}
		
		return list;
	}
}

